height/**
 * Created by sail on 2017/6/1.
 */
import WeCropper from '../we-cropper/we-cropper.js'


const app = getApp();
var device = wx.getSystemInfoSync()
var top = app.globalData.startBarHeight + app.globalData.navgationHeight;
var width = device.windowWidth
var height = device.windowHeight - top - 50
var img = '';

console.log(top)
console.log(height)
Page({
  data: {
    url: app.globalData.url,
    color: app.globalData.color,
    startBarHeight: app.globalData.startBarHeight,
    navgationHeight: app.globalData.navgationHeight,
    height: device.windowHeight - top,
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    }
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    if (img != "") {
      this.wecropper.getCropperImage((src) => {
        if (src) {
          console.log(src)
          var that = this;
          wx.uploadFile({
            url: app.globalData.apiurl + 'api/student/headimg_upload',
            filePath: src,
            name: 'file',
            formData: {
              apiToken: wx.getStorageSync('Stoken'),
              apiUser: wx.getStorageSync('student_id'),
            },
            success(res) {
              
              console.log(JSON.parse(res.data))
              if (JSON.parse(res.data).code == 100) {
                //请登录
                wx.showModal({
                  title: '',
                  content: '登录失效，请重新登录！',
                  showCancel: false,
                  confirmText: '我知道了',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.setStorageSync('Ttoken', "");
                      wx.setStorageSync('Stoken', "");
                      wx.reLaunch({
                        url: '../index/index',
                      })
                    }
                  }
                })
              } else if (JSON.parse(res.data).code == 0) {
                var pages = getCurrentPages();
                if (pages.length > 1) {
                  //上一个页面实例对象
                  var prePage = pages[pages.length - 2];
                  //关键在这里
                  prePage.setData({
                    headimgurl: JSON.parse(res.data).data
                  })
                  wx.navigateBack({
                    delta: 1
                  })
                }
              } else {
                wx.showModal({
                  title: '',
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: '我知道了',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    }
                  }
                })
              }
            }
          })
          // wx.previewImage({
          //   current: '', // 当前显示图片的http链接
          //   urls: [src] // 需要预览的图片http链接列表
          // })
        } else {
          console.log('获取图片地址失败，请稍后重试')
        }
      })
    } else {
      wx.showToast({
        title: '请先上传图片',
        icon: 'loading',
        duration: 1000
      })
      setTimeout(function() {
        wx.hideToast()
      }, 1000)
    }

  },
  uploadTap() {
    const self = this

    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值

        self.wecropper.pushOrign(src)
        img = 1
      }
    })
  },
  onLoad(option) {
    const {
      cropperOpt
    } = this.data
    // console.log("aaa + " + JSON.stringify(this.data))
    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas()
  },
  //返回上一页
  top_return: function() {
    var pages = getCurrentPages();
    //console.log(pages)
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //关键在这里
      prePage.onLoad()
      wx.navigateBack({
        delta: 1
      })
    }
  },
})